package org.frankkie.zooi;

import android.app.Activity;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import java.io.PrintStream;

/**
 * @author frankkie
 */
public class GPSActivity extends Activity {

  public GPSActivity() {
    //Constructor
    System.out.println("GPSActivity.<Constructor>");
    //this.setVisible(true);
  }
  static LocationManager locationManager;
  static TextView tv2;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    System.setErr(new PrintStream(new SystemOut("System.err")));
    System.setOut(new PrintStream(new SystemOut("System.out")));
    System.out.println("GPSActivity.onCreate");
    // ToDo add your GUI initialization code here
    this.setContentView(R.layout.gps);
    tv2 = (TextView) findViewById(R.id.tv2);
    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates("gps",100,10,new LocationListener() {

      public void onLocationChanged(Location arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
        doeMaar();
      }

      public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        //throw new UnsupportedOperationException("Not supported yet.");
        doeMaar();
      }

      public void onProviderEnabled(String arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
      }

      public void onProviderDisabled(String arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
      }
    });
    Button startKnop = (Button) findViewById(R.id.startKnop);

    startKnop.setOnClickListener(new OnClickListener() {

      public void onClick(View arg0) {
        //throw new UnsupportedOperationException("Not supported yet.");
        //klokje.scheduleAtFixedRate(taak, 1000, 1000);
        try {
          doeMaar();
        } catch (Exception e) {
        }
      }
    });

  }

  public static void doeMaar() {
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //getCurrentLocation("gps");
    GpsStatus gpsStatus = locationManager.getGpsStatus(null);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
      addStr("Provider is Enabled");
    } else {
      addStr("Provider is not Enabled");
    }
    addStr(">>GPSSTATUS<<");
    addStr("getMaxSatellites:" + gpsStatus.getMaxSatellites());
    addStr("getTimeToFirstFix:" + gpsStatus.getTimeToFirstFix());
////
    addStr(">>LOCATION<<");
    addStr("getAccuracy:" + location.getAccuracy());
    addStr("getAltitude:" + location.getAltitude());
    addStr("getBearing:" + location.getBearing());
    addStr("getExtras:" + location.getExtras());
    addStr("getLatitude:" + location.getLatitude());
    addStr("getLongtitude:" + location.getLongitude());
    addStr("getProvider:" + location.getProvider());
    addStr("getSpeed:" + location.getSpeed() + " (speed*3,6="+(location.getSpeed()*3.6)+")");
    addStr("getTime:" + location.getTime());
    addStr("hasAccuracy:" + location.hasAccuracy());
    addStr("hasAltitude:" + location.hasAltitude());
    addStr("hasBearing:" + location.hasBearing());
    addStr("hasSpeed:" + location.hasSpeed());
    addStr("System.currentTimeMillis:"+System.currentTimeMillis());
    addStr("=============================");
  }

  public static void addStr(Object str) {
    String a = str + "\n" + tv2.getText();
    tv2.setText(a);
  }
}
